using System.Globalization;
using Avalonia.Data.Converters;
using Avalonia.Markup.Xaml;
using JMCOM.Components.EpsonRobot.NodeFlow.Nodebehind.BlackBoard;

namespace JMCOM.Components.EpsonRobot.NodeFlow.Nodebehind;

public class BlackboardKeyEditorConverter : MarkupExtension, IMultiValueConverter
{
    public bool CanChangeInputType { get; set; }

    public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
    {
        if (values.Count >= 2 && values[0] is ICollection<BlackboardKeyViewModel> availableKeys && values[1] is BlackboardKeyViewModel target)
        {
            return new BlackboardKeyEditorViewModel
            {
                AvailableKeys = availableKeys,
                Target = target,
                IsEditing = values.Count >= 3 && values[2] is bool b && b,
                CanChangeInputType = CanChangeInputType && (target.Type != BlackboardKeyType.Object || target.CanChangeType),
                CanChangeKeyType = target.CanChangeType
            };
        }

        return values;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    public override object ProvideValue(IServiceProvider serviceProvider) => this;
}